
package com.hlkj.pay.controller.merchant;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import cn.hutool.core.bean.BeanUtil;
import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.pojo.PageResult;
import com.hlkj.framework.common.util.collection.CollectionUtils;
import com.hlkj.framework.common.util.json.JsonUtils;
import com.hlkj.pay.app.merchant.IMerchantInfoApplicationAppService;
import com.hlkj.pay.controller.BaseController;
import com.hlkj.pay.dto.merchant.MerchantAppChannelListDto;
import com.hlkj.pay.dto.merchant.MerchantAppQueryDto;
import com.hlkj.pay.dto.pay.PayWayQueryDto;
import com.hlkj.pay.enums.CommonEnum;
import com.hlkj.pay.infrastructure.model.merchant.MerchantAppChannelDO;
import com.hlkj.pay.infrastructure.model.merchant.MerchantConfigDO;
import com.hlkj.pay.infrastructure.model.pay.PayChannelDO;
import com.hlkj.pay.infrastructure.model.pay.PayWayDO;
import com.hlkj.pay.infrastructure.model.pay.ProviderInfoDO;
import com.hlkj.pay.service.merchant.IMerchantApplicationService;
import com.hlkj.pay.service.merchant.IMerchantConfigService;
import com.hlkj.pay.service.merchant.IMerchantService;
import com.hlkj.pay.service.pay.IPayChannelService;
import com.hlkj.pay.service.pay.IPayWayService;
import com.hlkj.pay.service.pay.IProviderService;
import com.hlkj.pay.vo.common.req.DeleteCodeReq;
import com.hlkj.pay.vo.merchant.req.UpdateStatusReq;
import com.hlkj.pay.vo.merchant.req.app.MerchantAppPageReq;
import com.hlkj.pay.vo.merchant.resp.app.MerchantAppChannelDetailResp;
import com.hlkj.pay.vo.merchant.resp.app.MerchantAppChannelListResp;
import com.hlkj.pay.vo.merchant.resp.info.MerchantFeeConfigResp;
import com.hlkj.pay.vo.pay.resp.PayWayResp;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author HlpayTeam
 * @date 2024/09/12 11:32
 */
@Tag(name = "商户应用通道管理")
@RestController
@RequestMapping("/v1/merchant/app/channel")
@RequiredArgsConstructor
public class MerchantAppChannelController extends BaseController {

    private final IMerchantApplicationService merchantApplicationService;

    private final IMerchantInfoApplicationAppService merchantInfoApplicationAppService;

    private final IMerchantService merchantService;
    private final IMerchantConfigService merchantConfigService;

    private final IProviderService providerService;

    private final IPayChannelService payChannelService;

    private final IPayWayService payWayService;

    @Operation(summary = "通道列表分页查询")
    @GetMapping("")
    CommonResult<PageResult<MerchantAppChannelListResp>> queryPage(@Validated MerchantAppPageReq merchantAppPageReq) {
        MerchantAppQueryDto merchantInfoQueryDO = BeanUtil.copyProperties(merchantAppPageReq, MerchantAppQueryDto.class);
        merchantInfoQueryDO.setAppName(null);
        merchantInfoQueryDO.setLikeAppName(merchantAppPageReq.getAppName());
        processQueryDate(merchantAppPageReq,merchantInfoQueryDO);
        PageResult<MerchantAppChannelListDto> page = merchantApplicationService.queryChannelPage(merchantInfoQueryDO);
        List<MerchantAppChannelListDto> list = page.getList();
        if (CollectionUtils.isEmpty(list)) {
            return buildPageResult(page, Collections.emptyList());
        }
        List<MerchantAppChannelListResp> merchantAppChannelListResps = BeanUtil.copyToList(list, MerchantAppChannelListResp.class);
        processProvideList(merchantAppChannelListResps);
        return buildPageResult(page, merchantAppChannelListResps);
    }

    @Operation(summary = "通道列表不分页查询")
    @GetMapping("/list")
    CommonResult<List<MerchantAppChannelListResp>> queryList(@Validated MerchantAppPageReq merchantAppPageReq) {
        MerchantAppQueryDto merchantInfoQueryDO = new MerchantAppQueryDto();
        merchantInfoQueryDO.setAppId(merchantAppPageReq.getAppId());
        merchantInfoQueryDO.setPageNo(merchantAppPageReq.getPageNo());
        merchantInfoQueryDO.setPageSize(merchantAppPageReq.getPageSize());
        List<MerchantAppChannelDO> list = merchantApplicationService.queryChannelInfoList(merchantInfoQueryDO);
        if (CollectionUtils.isEmpty(list)) {
            return CommonResult.success(Collections.emptyList());
        }
        List<MerchantAppChannelListResp> merchantAppChannelListResps = BeanUtil.copyToList(list, MerchantAppChannelListResp.class);
        processProvideList(merchantAppChannelListResps);
        return CommonResult.success(merchantAppChannelListResps);
    }

    @Operation(summary = "通道详情")
    @GetMapping("/detail")
    CommonResult<MerchantAppChannelDetailResp> detail(@RequestParam("code") String code) {
        MerchantAppChannelDO merchantAppDO = merchantApplicationService.queryAppChannelDetail(code);
        if (merchantAppDO == null) {
            return CommonResult.success();
        }
        MerchantAppChannelDetailResp merchantAppChannelDetailResp = BeanUtil.copyProperties(merchantAppDO, MerchantAppChannelDetailResp.class);
        // 获取商户已经开启的通道列表
        MerchantConfigDO merchantConfigDO = merchantConfigService.queryConfig(merchantAppChannelDetailResp.getSn());
        if (merchantConfigDO != null) {
            if (StringUtils.hasText(merchantConfigDO.getFeeConfig())) {
                List<MerchantFeeConfigResp> providerChannelFeeConfigList = JsonUtils.parseArray(merchantConfigDO.getFeeConfig(), MerchantFeeConfigResp.class);
                List<String> payWayCodes = providerChannelFeeConfigList.stream()
                        .filter(merchantFeeConfigResp -> CommonEnum.YES_NO_TYPE.YES.getCode().equals(merchantFeeConfigResp.getStatus()))
                        .map(merchantFeeConfigResp -> merchantFeeConfigResp.getPayWayCode()).collect(Collectors.toList());
                PayWayQueryDto payWayQueryDto = new PayWayQueryDto();
                payWayQueryDto.setCodes(payWayCodes);
                List<PayWayDO> payWayList = payWayService.queryList(payWayQueryDto);
                merchantAppChannelDetailResp.setMerchantPayWays(BeanUtil.copyToList(payWayList, PayWayResp.class));
            }
        }

        if (StringUtils.hasText(merchantAppChannelDetailResp.getChannelCode())) {
            PayChannelDO channelDO = payChannelService.queryByCode(merchantAppChannelDetailResp.getChannelCode());
            if (channelDO != null) merchantAppChannelDetailResp.setChannelName(channelDO.getName());
        }

        if (StringUtils.hasText(merchantAppChannelDetailResp.getProviderCode())) {
            ProviderInfoDO providerInfoDO = providerService.queryByCode(merchantAppChannelDetailResp.getProviderCode());
            if (providerInfoDO != null) merchantAppChannelDetailResp.setProviderName(providerInfoDO.getName());
        }
        return CommonResult.success(merchantAppChannelDetailResp);
    }

    @Operation(summary = "新增通道")
    @PostMapping("")
    CommonResult<String> saveMerchantAppChannel(@Validated @RequestBody MerchantAppChannelDetailResp merchantAppChannelDetailResp) {
        return merchantInfoApplicationAppService.saveMerchantAppChannel(merchantAppChannelDetailResp);
    }

    @Operation(summary = "更新通道")
    @PutMapping("")
    CommonResult<Void> updateMerchantAppChannel(@Validated @RequestBody MerchantAppChannelDetailResp merchantAppChannelDetailResp) {
        return merchantInfoApplicationAppService.updateMerchantAppChannel(merchantAppChannelDetailResp);
    }

    @Operation(summary = "更新通道状态")
    @PostMapping("/status")
    CommonResult<Void> updateStatus(@Validated @RequestBody UpdateStatusReq updateStatusReq) {
        return merchantInfoApplicationAppService.updateMerchantAppChannelStatus(updateStatusReq);
    }
    @Operation(summary = "更新通道轮询状态")
    @PostMapping("/limitRouter")
    CommonResult<Void> updateLimitRouter(@Validated @RequestBody UpdateStatusReq updateStatusReq) {
        return merchantInfoApplicationAppService.updateLimitRouter(updateStatusReq);
    }

    @Operation(summary = "删除通道")
    @DeleteMapping("")
    CommonResult<Void> delMerchantAppChannel(@Validated @RequestBody DeleteCodeReq deleteCodeReq) {
        return merchantInfoApplicationAppService.delMerchantAppChannel(deleteCodeReq.getCode());
    }

}
